package com.slg.android.listenbook.biz.bookcity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.slg.android.listenbook.R;
import com.slg.android.listenbook.biz.bookcity.adapter.CategoryDetailAdapter;
import com.slg.android.listenbook.biz.bookcity.model.CategoryDetail;
import com.slg.android.listenbook.biz.home.BaseActivity;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.LinkedList;

public class CategoryDetailActivity extends BaseActivity implements AdapterView.OnItemClickListener {

    private LinkedList<CategoryDetail> data;

    private GridView gridView;
    private CategoryDetailAdapter adapter;

    //请求路径
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_category_detail);

        ImageView imgView = (ImageView) findViewById(R.id.title_bar_search_img);
        imgView.setVisibility(View.INVISIBLE);
        Intent intent = getIntent();
        String title = intent.getStringExtra("title");
        setTitle(title);
        String type = intent.getStringExtra("type");

        gridView = (GridView) findViewById(R.id.category_detail_activity_grid_view);

        if(gridView != null) {
            data = new LinkedList<CategoryDetail>();
            adapter = new CategoryDetailAdapter(this,data);
            gridView.setAdapter(adapter);
            //网络获取数据名称
            HttpUtils httpUtils = new HttpUtils();

            httpUtils.send(HttpRequest.HttpMethod.GET,
                    "http://117.25.143.73/yyting/bookclient/ClientTypeResource.action?type="+type+"&pageNum=1&pageSize=500&token=DpPEKRjVAkk2j9DtIqO_H8vwQMcUBMNp-zdukwv7lbY*&imei=ODYwMzA4MDI4ODU1OTg1"
                    , new RequestCallBack<String>() {
                        @Override
                        public void onSuccess(ResponseInfo<String> objectResponseInfo) {

                            String result = objectResponseInfo.result;
                            LinkedList<CategoryDetail> list = new LinkedList<CategoryDetail>();
                            try {
                                JSONObject jsonObject = new JSONObject(result);
                                JSONArray jsonArray = jsonObject.getJSONArray("list");
                                for (int i = 0; i < jsonArray.length(); i++) {

                                    JSONObject object = jsonArray.getJSONObject(i);
                                    CategoryDetail categoryDetail = new CategoryDetail();
                                    categoryDetail.parseCategoryDetail(object);
                                    list.add(categoryDetail);
                                }
                                data.addAll(list);
                                adapter.notifyDataSetChanged();
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }

                        @Override
                        public void onFailure(HttpException e, String s) {
                            Toast.makeText(CategoryDetailActivity.this, "网络出错", Toast.LENGTH_SHORT).show();
                        }
                    });
        }

        gridView.setOnItemClickListener(this);

    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        CategoryDetail categoryDetail = data.get(position);
        String typeId = categoryDetail.getId();
        String title = categoryDetail.getName();
        Intent intent = new Intent(this, CategoryListActivity.class);
        intent.putExtra("id",typeId);
        intent.putExtra("title",title);
        intent.putExtra("list","category");
        startActivity(intent);

    }
}
